#include "lable_weather.h"
#include <QDebug>

Lable_weather::Lable_weather(QWidget *parent) : QLabel(parent)
{

}

void Lable_weather::set_temp(const int * temp)
{
    for (int i = 0 ; i < 7 ; i ++) {
        this->temp[i] = temp[i];
    }
}

void Lable_weather::set_high_low_temp(bool b)
{
    // 高温
    if(b == true)
    {
        lind_color.setRgb(253,239, 18);
    }
    else
    {
        lind_color.setRgb( 37,222,251 );
    }
}

// 双击事件
void Lable_weather::mouseDoubleClickEvent(QMouseEvent *event)
{
    this->update();
}

// 天气绘制
void Lable_weather::paintEvent(QPaintEvent *event)
{
    // 设计画板
    QPainter Painter(this);

    // 设置画笔
    QPen pen(this->lind_color);
    pen.setWidth(3);
    pen.setStyle(Qt::SolidLine);

    // 设置画刷
    QBrush brush(this->lind_color);
    brush.setStyle(Qt::SolidPattern);

    // 为画布设置画笔和画刷
    Painter.setPen(pen);
    Painter.setBrush(brush);

    // 计算坐标
    int max_temp  = 0;
    int min_temp  = this->temp[0];
    int unit_temp = 0;
    int unit_time = 0;
    QString temp_str;
    QPoint p_temp[7];

    for (int i = 0 ; i < 6 ; i ++) {
        max_temp = (max_temp > this->temp[i+1] ? max_temp : this->temp[i+1]);
        min_temp = (min_temp < this->temp[i+1] ? min_temp : this->temp[i+1]);
    }

    //  每个温度的单位  =  label 总高度  /  温度差
    unit_temp = this->height() / ((max_temp - min_temp) + 4);

    //  每个时间的单位 =   label 总宽度  / 7
    unit_time = this->width() / 8;


    for (int i = 0 ; i < 7 ; i++) {
        p_temp[i].setX(unit_time + (unit_time * i + 1));
        p_temp[i].setY((max_temp - this->temp[i] + 2) * (unit_temp )); //
    }

    // 画线
    Painter.drawPolyline ( p_temp , 7 );
    // 画圆
    for (int i = 0 ; i < 7 ; i ++ ) {
        // 绘制圆点
        Painter.drawEllipse( p_temp[i].x() - 5 , p_temp[i].y() - 5 , 10 , 10  );
        // 绘制文字
        Painter.drawText( p_temp[i].x() - 5 , p_temp[i].y() - 8 , temp_str.number(this->temp[i]) + "℃");
    }

}
